/** ============================================================================================== **/
/**						GESTIONE DELLE PIANTE									 				   **/	
/** ============================================================================================== **/
//Servizio
BootApplication.service("ViewProfileService",
			    ['configuration', '$rootScope', 'ngDialog', 'DateService', 'log', 'DialogService', '$translate', '$http', '$q',
		function( configuration ,  $rootScope ,  ngDialog ,  DateService,   log ,  DialogService ,  $translate ,  $http ,  $q) {
	  
	console.log("ViewProfileService");
	
	/**
	 * Questo metodo recupera il profilo dell'utente
	 **/
	this.getUserProfile = function( ) {
		 var _defer = $q.defer();
		 var request = {
				  method	: 'POST'
				  ,url		: configuration.getParam("Constants").ContextPath + "userCtrl/getUser.pjson"
				  ,param  	: {}
		 };
  	  	 $http(request).then(function(response){_defer.resolve(response);}, function(errorResponse){_defer.resolve(null);});
  	  	 return _defer.promise;
	 }
	
	/**
	 * Questo metodo salva le modifiche al profilo
	 **/
	this.saveUser = function( _userModel ) {
		 _newModel = {};
		 if (_userModel.userId!=undefined)			_newModel.id			= _userModel.userId;
		 if (_userModel.firstname!=undefined) 		_newModel.firstName		= _userModel.firstname;
		 if (_userModel.lastname!=undefined) 		_newModel.lastName		= _userModel.lastname;
		 if (_userModel.email!=undefined)			_newModel.email 		= _userModel.email;
		 if (_userModel.dateBirth!=undefined)		_newModel.dateBirthStr	= 
			 												_userModel.dateBirth.dayOfMonth+"/"+
			 												(_userModel.dateBirth.monthValue<10?"0"+_userModel.dateBirth.monthValue:_userModel.dateBirth.monthValue)+"/"+
			 												_userModel.dateBirth.year;
		 
		 var _defer = $q.defer();
		 var request = {
				  method	: 'POST'
				  ,url		: configuration.getParam("Constants").ContextPath + "userCtrl/saveUser.pjson"
				  ,data  	: _newModel
		 };
  	  	 $http(request).then(function(response){_defer.resolve(response);}, function(errorResponse){_defer.resolve(null);});
  	  	 return _defer.promise;
	 }
}]);